package com.zhuliwei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuliwei.domain.ResponseResult;
import com.zhuliwei.domain.entity.Sentence;
import com.zhuliwei.domain.vo.PageVo;
import com.zhuliwei.enums.AppHttpCodeEnum;
import com.zhuliwei.exception.SystemException;
import com.zhuliwei.mapper.SentenceMapper;
import com.zhuliwei.service.SentenceService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Random;

/**
 * 句子接口实现类
 *
 * @author 朱力维
 * @time 2023/5/25 16:08
 **/
@Service
public class SentenceServiceImpl extends ServiceImpl<SentenceMapper, Sentence> implements SentenceService {
    @Override
    public ResponseResult<Sentence> getRandomContent() {
        List<Sentence> sentenceList = list();
        Object[] sentences = sentenceList.toArray();
        Random random = new Random();
        int index = random.nextInt(sentences.length - 1)+1;
        return ResponseResult.okResult(sentences[index]);
    }

    @Override
    public ResponseResult<Sentence> getSentences(Integer pageNum, Integer pageSize, Sentence sentence) {
        //分页查询
        LambdaQueryWrapper<Sentence> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(sentence.getContent()), Sentence::getContent, sentence.getContent());
        queryWrapper.like(StringUtils.hasText(sentence.getSource()), Sentence::getSource, sentence.getSource());
        queryWrapper.orderByDesc(Sentence::getCreateTime);
        Page<Sentence> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, queryWrapper);
        List<Sentence> sentences = page.getRecords();
        //封装数据返回
        PageVo pageVo = new PageVo(sentences, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addSentence(Sentence sentence) {
        if (!StringUtils.hasText(sentence.getContent())) {
            throw new SystemException(AppHttpCodeEnum.SENTENCE_CONTENT_NULL);
        }
        if (!StringUtils.hasText(sentence.getSource())) {
            throw new SystemException(AppHttpCodeEnum.SENTENCE_SOURCE_NULL);
        }
        save(sentence);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delSentence(List<String> ids) {
        removeByIds(ids);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateSentence(Sentence sentence) {
        if (!StringUtils.hasText(sentence.getContent())) {
            throw new SystemException(AppHttpCodeEnum.SENTENCE_CONTENT_NULL);
        }
        if (!StringUtils.hasText(sentence.getSource())) {
            throw new SystemException(AppHttpCodeEnum.SENTENCE_SOURCE_NULL);
        }
        updateById(sentence);
        return ResponseResult.okResult();
    }
}

